package com.ysqcould.approval.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.ysqcould.allocation.domain.WxUser;
import com.ysqcould.allocation.mapper.WxUserMapper;
import com.ysqcould.approval.domain.GeneralTask;
import com.ysqcould.approval.domain.vo.GeneralTaskVo;
import com.ysqcould.approval.mapper.GeneralTaskMapper;
import com.ysqcould.approval.service.IGeneralTaskService;
import com.ysqcould.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-29
 */
@Service
public class GeneralTaskServiceImpl implements IGeneralTaskService
{
    @Autowired
    private GeneralTaskMapper generalTaskMapper;

    @Autowired
    private WxUserMapper userMapper;

    /**
     * 查询通用任务
     * 
     * @param id 通用任务主键
     * @return 通用任务
     */
    @Override
    public GeneralTask selectGeneralTaskById(Long id)
    {
        return generalTaskMapper.selectGeneralTaskById(id);
    }

    /**
     * 查询通用任务列表
     * 
     * @param generalTask 通用任务
     * @return 通用任务
     */
    @Override
    public List<GeneralTaskVo> selectGeneralTaskList(GeneralTask generalTask)
    {
        List<GeneralTaskVo> generalTasks = generalTaskMapper.selectGeneralTaskList(generalTask);
        generalTasks.forEach(p->{
            List<String>list=new ArrayList<>();
            if (ObjectUtil.isNotEmpty(p.getPrincipalIds())){
                for (String s : p.getPrincipalIds().split(";")) {
                    WxUser wxUser = userMapper.selectWxUserById(Long.parseLong(s));
                    if (ObjectUtil.isNotEmpty(wxUser)){
                        list.add(wxUser.getRealName());
                    }
                }
                if (list.size()>0){
                    p.setApplyName(list.stream().collect(Collectors.joining(",")));
                }
            }
        });
        return generalTasks;
    }

    /**
     * 新增通用任务
     * 
     * @param generalTask 通用任务
     * @return 结果
     */
    @Override
    public int insertGeneralTask(GeneralTask generalTask)
    {
        generalTask.setCreateTime(DateUtils.getNowDate());
        return generalTaskMapper.insertGeneralTask(generalTask);
    }

    /**
     * 修改通用任务
     * 
     * @param generalTask 通用任务
     * @return 结果
     */
    @Override
    public int updateGeneralTask(GeneralTask generalTask)
    {
        generalTask.setUpdateTime(DateUtils.getNowDate());
        return generalTaskMapper.updateGeneralTask(generalTask);
    }

    /**
     * 批量删除通用任务
     * 
     * @param ids 需要删除的通用任务主键
     * @return 结果
     */
    @Override
    public int deleteGeneralTaskByIds(Long[] ids)
    {
        return generalTaskMapper.deleteGeneralTaskByIds(ids);
    }

    /**
     * 删除通用任务信息
     * 
     * @param id 通用任务主键
     * @return 结果
     */
    @Override
    public int deleteGeneralTaskById(Long id)
    {
        return generalTaskMapper.deleteGeneralTaskById(id);
    }
}
